<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/8/26
 * Time: 22:00
 */

namespace Application\Common\Controller;


use J\Schema\Syntax;
use J\Exception;
use J\Intent;
use J\MVC\Actor;
use J\MVC\InvokeMethod;

class AutoActor extends Actor
{
    use InvokeMethod;

    /**
     * @var BaseController
     */
    protected $controller;

    /**
     * 没有注册的操作，默认被禁止
     * @inheritdoc
     */
    protected function act(Intent $intent)
    {
        if ($this->controller->getInformation($intent, null, -1) === -1) return false;

        return $this->invokeIntentMethod($intent);
    }

    /**
     * 配合 InvokeMethod
     * 
     * @param string $key
     * @param mixed $default
     * @param bool $withUpper
     */
    protected function get($key, $default = null, $withUpper = true)
    {
        $this->controller->get($key, $default, $withUpper);
    }


    //----------------------------------------RestFull----------------------------------------

    /**
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionPost(Intent $intent)
    {
        $this->optionApiAdd($intent);
    }

    /**
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionPut(Intent $intent)
    {
        $this->optionApiEdit($intent);
    }

    /**
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionDelete(Intent $intent)
    {
        $this->optionApiDelete($intent);
    }

    //----------------------------------------Traditional---------------------------------------

    /**
     * 删除条目
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiDelete(Intent $intent)
    {
        $recycle = $this->controller->getInformation($intent, 'recycle', true);
        $this->controller->delete($recycle, $this->filterAdd([Syntax::KEY_ID]));
        $intent->ajax(true, '已删除');
    }

    /**
     * 添加条目
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiAdd(Intent $intent)
    {
        $this->controller->add($this->filterAdd([]));
        $intent->ajax(true, '添加成功');
    }

    /**
     * 修改条目
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiEdit(Intent $intent)
    {
        $this->controller->edit(
            $this->filterAdd([Syntax::KEY_ID]),
            $this->filterRemove($this->getElements($intent))
        );
        $intent->ajax(true, '已修改');
    }

    /**
     * 批量删除
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiRemove(Intent $intent)
    {
        $this->controller->delete(true, $this->filterAdd([Syntax::KEY_ID]));
        $intent->ajax(true, '已删除');
    }

    /**
     * 恢复条目
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiRecovery(Intent $intent)
    {
        $this->controller->recovery($this->filterAdd([]));
        $intent->ajax(true, '已恢复');
    }

    /**
     * 清空回收站
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiClean(Intent $intent)
    {
        $this->controller->clean($this->filterAdd([]));
        $intent->ajax(true, '已清除');
    }

    /**
     * 回收站条目
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiRecycle(Intent $intent)
    {
        $intent->value = $this->controller->recycle($this->getElements($intent));
    }

    protected function getElements(Intent $intent)
    {
        return $this->controller->getInformation(
            $intent,
            Syntax::KEY_ELEMENTS,
            []
        );
    }

}